package controller;

import Interceptor.LoginFormValidator;
import Interceptor.NavInterceptor;
import Interceptor.RegeistValidator;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.Kv;
import com.jfinal.kit.LogKit;
import com.jfinal.plugin.activerecord.Db;
import model.Topic;
import model.User;

import java.util.List;

public class MainController extends Controller {
    @Before(NavInterceptor.class)
    public void index() {
        String sql = Db.getSql("getHotTopics");

        List<Topic> topics = Topic.dao.find(sql);
        setAttr("topics", topics);
        renderFreeMarker("main.ftl");
    }
    @Before(NavInterceptor.class)
    public void register()
    {
        renderFreeMarker("register.ftl");
    }
    @Before(RegeistValidator.class)
    public void doRegister()
    {
        String username = getPara("username");
        String password = getPara("password");
        String nickname = getPara("nickname");

        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setNickname(nickname);
        boolean success = false;
        try {
            user.save();
            success = true;
        } catch (Exception e) {
            LogKit.error("用户注册失败，原因是:" + e.getMessage());
        }
        String message = success ? "注册成功" : "注册失败";
        Kv result = Kv.by("success", success).set("message", message);
        renderJson(result);
    }
    //@Before(NavInterceptor.class)
    public void login(){renderFreeMarker("login.ftl");}
    @Before(LoginFormValidator.class)
    public void loginCheck() {
        String username = getPara("username");
        String password = getPara("password");
        String passwordMd5 = HashKit.md5(password);
        boolean success = false;
        String message;
        List<User> users = User.dao.find("SELECT * FROM t_user WHERE username = ? AND password = ?",
                username, passwordMd5);
        //登录成功
        if (users.size() != 0) {
            //跳转到主页面
            redirect("/main");
            //往session中放入一个key为user的变量，值为用户登录的用户名
            setSessionAttr("user", users.get(0));
            message = "登录成功";
            success = true;
        } else {
            //登录失败
            //setAttr("errmsg", "用户名或密码错误");

            message = "用户名或密码错误";

            //renderFreeMarker("login.ftl");
        }
        Kv result = Kv.by("success", success).set("message", message);
        renderJson(result);
    }

    public void logout(){
        removeSessionAttr("user");
        redirect("/login");
    }

/*    //@Before(Login.class)
    public void main() {
        //利用session来登录主页面，如果username为null，则进入login中登录，否则就进入主页面
        String sql = "SELECT\n" +
                "t_topic.*,\n" +
                "t.*,\n" +
                "t_category.name AS category_name\n" +
                "FROM\n" +
                "t_topic\n" +
                "LEFT JOIN (\n" +
                "SELECT\n" +
                "topic_id,\n" +
                "COUNT(id) AS reply_count\n" +
                "FROM\n" +
                "t_reply\n" +
                "GROUP BY\n" +
                "topic_id\n" +
                ") AS T ON t_topic.id = T.topic_id\n" +
                "INNER JOIN t_category\n" +
                "ON t_topic.category_id = t_category.id\n" +
                "ORDER BY reply_count DESC";

        List<Topic> topics = Topic.dao.find(sql);
        setAttr("topics", topics);


        List<Category> categories = Category.dao.find("SELECT * FROM t_category");
        setAttr("categories", categories);
        renderFreeMarker("login-result.ftl");
    }*/
    @Before(NavInterceptor.class)
    public void demo(){

        renderFreeMarker("demo.ftl");
    }
    public void captcha(){
        renderCaptcha();
    }
}
